package com.market.service.admin.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.market.bean.MarketAd;
import com.market.bean.MarketAdExample;
import com.market.bean.common.BasePageInfo;
import com.market.bean.common.CommonData;
import com.market.mapper.MarketAdMapper;
import com.market.service.admin.AdminAdService;
import com.market.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author fan
 * @Description:
 * @since 2022/12/28 20:53
 */
@Service
public class AdminAdServiceImpl implements AdminAdService {

    @Autowired
    MarketAdMapper adMapper;

    @Override
    public CommonData<MarketAd> list(BasePageInfo info, String name, String content) {
        PageHelper.startPage(info.getPage(), info.getLimit());
        MarketAdExample example = new MarketAdExample();
        //排序
        example.setOrderByClause(info.getSort() + " " + info.getOrder());
        MarketAdExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        if (!StringUtil.isEmpty(content)) {
            criteria.andContentLike("%" + content + "%");
        }
        criteria.andDeletedEqualTo(false);
        List<MarketAd> list = adMapper.selectByExample(example);
        PageInfo<MarketAd> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);
    }

    @Override
    public MarketAd create(MarketAd marketAd) {
        Date date = new Date();
        marketAd.setAddTime(date);
        marketAd.setUpdateTime(date);
        adMapper.insertSelectiveReturnId(marketAd);
        return marketAd;
    }

    @Override
    public MarketAd update(MarketAd marketAd) {
        Date date = new Date();
        marketAd.setUpdateTime(date);
        adMapper.updateByPrimaryKeySelective(marketAd);
        return marketAd;
    }

    @Override
    public void delete(MarketAd marketAd) {
        marketAd.setDeleted(true);
        adMapper.updateByPrimaryKeySelective(marketAd);
    }

}
